package com.wu.util;

import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;
import com.wu.pojo.ArticleDraft;
import com.wu.pojo.Articles;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-24 08:27
 */
@Component
public class ArticleConversionUtil {
    /**
     * 转换md格式为html
     *
     * @return md->html
     */
    public String mdToHtml(String md) {
        MutableDataSet options = new MutableDataSet();
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse(md);
        String html = renderer.render(document);
        return html;
    }

    /**
     * 将html转换成文本
     *
     * @param html
     * @return html->text
     */
    public String getTextByHtml(String html) {

        if (StringUtils.isEmpty(html)) {
            return "";
        }

        Document document = Jsoup.parse(html);
        Document.OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false);
        document.outputSettings(outputSettings);
        document.select("br").append("\\n");
        document.select("p").prepend("\\n");
        document.select("p").append("\\n");
        String newHtml = document.html().replaceAll("\\\\n", "\n");
        String plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings);
        String result = StringEscapeUtils.unescapeHtml(plainText.trim());
        return result;

    }


    /**
     * 截取文章的前 100个字
     *
     * @param articles
     * @return
     */
    public List<Articles> getArticleByOther(List<Articles> articles) {

        for (int i = 0; i < articles.size(); i++) {
            Articles article = articles.get(i);
            String main = article.getArticleContent();
            String html = mdToHtml(main);
            String text = getTextByHtml(html);
            article.setArticleContent(text);
//            articles.get(i).setArticleContent(text.substring(0, Math.min(text.length(), 100)));
        }
        return articles;
    }

    /**
     * 从数据库获取的数据转换成纯文本
     *
     * @param article
     * @return
     */
    public Articles getArticleByOther(Articles article) {
        String main = article.getArticleContent();
        String html = mdToHtml(main);
        String text = getTextByHtml(html);
        article.setArticleContent(text);
        return article;
    }

    public Articles getArticleByOther100(Articles article) {
        String main = article.getArticleContent();
        String html = mdToHtml(main);
        String text = getTextByHtml(html);
        article.setArticleContent(text.substring(0, Math.min(text.length(), 100)));
        return article;
    }

    /**
     * 截取草稿的前 100个字
     *
     * @param articles
     * @return
     */
    public List<ArticleDraft> getArticleDraftConversion(List<ArticleDraft> articles) {

        for (int i = 0; i < articles.size(); i++) {
            String main = articles.get(i).getArticleContent();
            String html = mdToHtml(main);
            String text = getTextByHtml(html);
            articles.get(i).setArticleContent(text.substring(0, Math.min(text.length(), 100)));
        }
        return articles;
    }


}
